import Vue from 'vue'
import Vuex from 'vuex'
import {
	get,
	post
} from '@/static/utils/request.js'
import {
	User
} from '@/static/utils/service'
Vue.use(Vuex);
const store = new Vuex.Store({
	state: {
		loginstate: false,
		userinfo: {}
	},
	mutations: {
		setLogin(state, userinfo) {
			if (userinfo) {
				state.loginstate = true
				state.userinfo = userinfo
				uni.setStorageSync('token', userinfo.token)
				uni.setStorageSync('userinfo', userinfo)
			} else {
				state.loginstate = false
				state.userinfo = {}
				uni.removeStorageSync('token')
				uni.removeStorageSync('userinfo')
			}
		},
		updateUserinfo(state, userinfo) {
			state.userinfo = userinfo
			uni.setStorageSync('userinfo', userinfo)
		}
	},
	actions: {
		//刷新用户信息
		async refreshUserinfo({
			commit
		}) {
			const res = await get(User.userinfo)
			if (res.code == 1) {
				commit('updateUserinfo', res.data)
			} else {
				commit('setLogin', false)
			}
		}
	},
	getters: {}
})

export default store